package model

import (
	"gorm.io/gorm"
	"fmt"
	
)

var condMap = map[string]string{
	"eq": "=",
	"ne": "!=",
	"gt": ">",
	"ge": ">=",
	"lt": "<",
	"le": "<=",
	"in": "in",
	"not in": "not in",
}

// 转换函数：输入缩写，返回操作符（或错误）
func ConvertCond(cond string) (string, error) {
    op, ok := condMap[cond]
    if !ok {
        return "", fmt.Errorf("不支持的条件缩写: %s", cond)
    }
    return op, nil
}

func ConditionMerage(query *gorm.DB,condition *Condition) {
	if condition == nil {
		return
	}
	
	AndConditionsMerage(query,condition.AndCondition)
	OrConditionsMerage(query,condition.OrCondition)
	BetweenConditionsMerage(query,condition.BetweenCondition)
	
	
}

func AndConditionsMerage(query *gorm.DB,conditions []*AndCondition){
	if conditions == nil {
		return
	}
	for _, v := range conditions {
		fmt.Printf("(")
		for _, vv := range v.Conditions {
			op, err := ConvertCond(vv.Condition)
			if err != nil {
				fmt.Printf("条件转换失败: %v", err)
				continue
			}
			query.Where(vv.Field+" "+op+" ?", vv.Value)
				
		}
		fmt.Printf(")")
	}
}

func OrConditionsMerage(query *gorm.DB,conditions []*OrCondition){
	if conditions == nil {
		return
	}
	for _, v := range conditions {
		fmt.Printf("(")
		for _, vv := range v.Conditions {
			op, err := ConvertCond(vv.Condition)
			if err != nil {
				fmt.Printf("条件转换失败: %v", err)
				continue
			}
			query.Where(vv.Field+" "+op+" ?", vv.Value)
		}
		fmt.Printf(")")
	}
}

func BetweenConditionsMerage(query *gorm.DB,conditions []*BetweenCondition){

}



